package pl.peterjack.model.jdo;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.appengine.api.datastore.Key;

import pl.peterjack.model.PMF;
import pl.peterjack.model.dane.Faktura;
import pl.peterjack.model.dane.FakturaProdukt;
import pl.peterjack.model.dane.Firma;
import pl.peterjack.model.dane.Klient;

public class FakturaJDO {
	
	public static Firma getFirma(Faktura faktura) {
		Long id = faktura.getFirma();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Firma firma  = pm.getObjectById(Firma.class, id);
		return firma; 
	}
	
	public static List<FakturaProdukt> getFakturaProdukty(Faktura faktura) {
		List<FakturaProdukt> results = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
	    Query query = pm.newQuery(FakturaProdukt.class, "faktura == fakturaPodana");
		query.declareParameters(Key.class.getName() + " fakturaPodana");
		results = (List<FakturaProdukt>) query.execute(faktura.getId());
		return results; 
		
	}
	
	public static Klient getKlient(Faktura faktura) {
		Long id = faktura.getKlient();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Klient klient = null; 
		try {
			klient = pm.getObjectById(Klient.class, id);
		} catch (Exception e) {
			// 
		}
		return klient; 		
	}

}
